import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hm_show_flutter/api/service/user_service.dart';
import 'package:hm_show_flutter/config/routers.dart';
import 'package:hm_show_flutter/constant/string.dart';
import 'package:hm_show_flutter/entity/upload_avatar_entity.dart';
import 'package:hm_show_flutter/entity/user.dart';
import 'package:hm_show_flutter/entity/user_info_entity.dart';
import 'package:hm_show_flutter/event/user_update_event.dart';
import 'package:hm_show_flutter/style/style.dart';
import 'package:hm_show_flutter/utils/navigator_util.dart';
import 'package:hm_show_flutter/utils/shared_preferences_util.dart';
import 'package:hm_show_flutter/utils/toast_util.dart';
import 'package:hm_show_flutter/widgets/AppLoadingDialog.dart';
import 'package:hm_show_flutter/widgets/icon_text_arrow.dart';
import 'package:hm_show_flutter/widgets/icon_text_arrow_it.dart';
import 'package:hm_show_flutter/widgets/loading_dialog.dart';
import 'package:hm_show_flutter/widgets/mall_icon.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:image_picker/image_picker.dart';

///编辑姓名
class EditNamePage extends StatefulWidget {
  @override
  _EditNamePageState createState() => _EditNamePageState();
}

class _EditNamePageState extends State<EditNamePage> {
  UserService _service = new UserService();
  UserModel _userModel;
  TextEditingController _accountTextControl = TextEditingController();

  @override
  void initState() {
    super.initState();

    SharedPreferencesUtils.getUser().then((user) {
      setState(() {
        _userModel = user;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    final email = TextFormField(
      keyboardType: TextInputType.emailAddress,
      autofocus: false,
      inputFormatters: [
        LengthLimitingTextInputFormatter(11), //限制输入长度不超过8位
      ],
      controller: _accountTextControl,
      decoration: InputDecoration(
        hintText: Strings.INPUT_NAME_HINT,
        hintStyle: TextStyle(fontSize: 12),
        contentPadding: EdgeInsets.fromLTRB(15.0, 12.0, 15.0, 12.0),
        focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(width: 1, color: AppColors.mainColor)),
        enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(width: 1, color: AppColors.grey)),
      ),
    );

    final submitButton = Container(
      width: double.infinity,
      padding: EdgeInsets.all(16),
      child: RaisedButton(
        onPressed: _submit,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
        padding: EdgeInsets.all(12),
        color: Colors.red,
        child: Text(Strings.SUBMIT,
            style: TextStyle(
              color: Colors.white,
            )),
      ),
    );

    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          automaticallyImplyLeading: true,
          centerTitle: true,
          iconTheme: IconThemeData(color: Colors.white),
          title: Text(
            "编辑姓名",
            style: TextStyle(color: Colors.white, fontSize: 18),
          ),
          backgroundColor: AppColors.mainColor,
        ),
        preferredSize: Size.fromHeight(40),
      ),
      body: _userModel == null
          ? LoadingDialog()
          : Container(
              color: Colors.grey[100],
              child: Column(
                children: <Widget>[
                  Padding(padding: EdgeInsets.only(top: 10)),
                  Container(
                    padding: EdgeInsets.only(left: 15, right: 15),
                    child: email,
                  ),
                  Expanded(child: Container()),
                  submitButton
                ],
              ),
            ),
    );
  }

  void _submit() {
    String content = _accountTextControl.text.toString();
    if (content != null || content.isNotEmpty) {
      _service.updateUserInfo(
          (success) {
            ToastUtil.showToast("修改成功");
            _service.getUserInfo((success) {
              UserInfoEntity model = success;
              SharedPreferencesUtils.saveUser(model.data.user);
            });
            userUpdateEventBus.fire(UserUpdateEvent());
            Navigator.pop(context);
          },
          name: content,
          onFail: (onFail) {
            ToastUtil.showToast("修改失败");
          });
    } else {
      ToastUtil.showToast("请输入内容");
    }
  }
}
